package com.cloudsigns.factorymanager2.produce.model.produce_team;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderResponse;
import com.cloudsigns.factorymanager2.produce.utils.ParseJson;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetProductWorkPartAssignDoneOrderListModel implements IGetProductWorkPartAssignDoneOrderListModel {

    @Override
    public void getProductWorkPartAssignDoneOrderList(final OnGetProductWorkPartAssignDoneOrderListListener onGetProductWorkPartAssignDoneOrderListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.GET_PRODUCT_WORKPART_ASSIGN_DONE_ORDER_LIST);
                HttpResponse response;
                try {
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("GetProductWorkPartAssig", "result=" + result);
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onGetProductWorkPartAssignDoneOrderListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("workPartResponseList"));
                            List<WorkPartOrderResponse> workPartOrderResponseList = new ArrayList<>();
                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        WorkPartOrderResponse workPartOrderResponse = new WorkPartOrderResponse();
                                        workPartOrderResponse.setEngineeringName(jsonObj.getString("engineeringName"));

                                        CommonEnum.ProduceOrderLevel level = ParseJson.parseProdutOrderLevel(jsonObj, "level");
                                        workPartOrderResponse.setLevel(level);

                                        workPartOrderResponse.setOrderId(jsonObj.getString("orderId"));
                                        workPartOrderResponse.setProductId(jsonObj.getString("productId"));
                                        workPartOrderResponse.setProductName(jsonObj.getString("productName"));
                                        workPartOrderResponse.setProjectName(jsonObj.getString("projectName"));
                                        CommonEnum.ProduceOrderStatus status = ParseJson.parseProdutOrderStatus(jsonObj, "status");
                                        workPartOrderResponse.setStatus(status);

                                        workPartOrderResponse.setType(jsonObj.getString("type"));
                                        workPartOrderResponse.setWorkPartId(jsonObj.getString("workPartId"));
                                        workPartOrderResponse.setWorkPartName(jsonObj.getString("workPartName"));
                                        workPartOrderResponse.setWorkPartOrderId(jsonObj.getString("workPartOrderId"));

                                        workPartOrderResponseList.add(workPartOrderResponse);
                                    }
                                }
                                onGetProductWorkPartAssignDoneOrderListListener.getDataSuccessed(workPartOrderResponseList);
                            }
                        } else {
                            onGetProductWorkPartAssignDoneOrderListListener.getDataFailed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnGetProductWorkPartAssignDoneOrderListListener {
        void getDataSuccessed(List<WorkPartOrderResponse> workPartOrderResponseList);

        void getDataFailed(String desc);
    }
}
